/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visao;

import Controle.Controlador;
import Modelo.Acompanhamento;
import Modelo.Pizza;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Eder
 */
public class NovoPedido extends javax.swing.JFrame {
    DefaultTableModel modelo;
    TelaPrincipal telaPrincipal;
    /**
     * Creates new form NovoPedido
     */
    public NovoPedido() {
        initComponents();
    }
    
    public NovoPedido(TelaPrincipal telaPrincipal, int codigoCliente) {
        initComponents();
        this.telaPrincipal = telaPrincipal;
        modelo = (DefaultTableModel) jtabelaPedido.getModel();
        jScrollPane1. getViewport().setBackground(Color.WHITE);
        jtabelaPedido.getColumnModel().getColumn( 0 ).setMaxWidth( 0 );  
        jtabelaPedido.getColumnModel().getColumn( 0 ).setMinWidth( 0 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 0 ).setMaxWidth( 0 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 0 ).setMinWidth( 0 );
        jtabelaPedido.getColumnModel().getColumn( 1 ).setMaxWidth( 60 );  
        jtabelaPedido.getColumnModel().getColumn( 1 ).setMinWidth( 0 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 1 ).setMaxWidth( 60 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 1 ).setMinWidth( 0 );
        jtabelaPedido.getColumnModel().getColumn( 3 ).setMaxWidth( 300 );  
        jtabelaPedido.getColumnModel().getColumn( 3 ).setMinWidth( 300 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 3 ).setMaxWidth( 300 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 3 ).setMinWidth( 0 );
        
        jentregaCBActionPerformed(null);
        Controlador.getPedidoAtual().setCodigo_cliente(codigoCliente);
        jtaxaEntregaTF.setText("R$   0.00");
        jdescontoTF.setText("R$   0.00");
    }
    
    public boolean getDadosVisao(){
        try {
//            jentregaCB
//            jtaxaEntregaTF
//            jdescontoTF
//            jvalorTotalTF
            if(validarDados()){
                Controlador.getPedidoAtual().setStatus("Ativo"); 
                Controlador.getPedidoAtual().setValor_entrega(getFormatoMoeda(jtaxaEntregaTF.getText()));
                Controlador.getPedidoAtual().setDesconto(getFormatoMoeda(jdescontoTF.getText()));
                Controlador.getPedidoAtual().setValor_total(getFormatoMoeda(jvalorTotalTF.getText()));
                String dataAtual = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(System.currentTimeMillis()));
                Controlador.getPedidoAtual().setData(dataAtual);

                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Obter os dados da tela do Pedido!\n"+e.getMessage(), "Erro!", 0);
            return false;
        }
    }
    
    public boolean validarDados(){
        try {
            if(getFormatoMoeda(jtaxaEntregaTF.getText(),"Taxa Entrega")==null){
                return false;
            }
            if(getFormatoMoeda(jdescontoTF.getText(),"Desconto")==null){
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private void imprimeDadosTabelas() {
        int linhaTabela = 0;
        
        //jTtabelaRP.setRowSorter(null); // desabilita o filtro
        
        for (int i = 0; i < Controlador.getPedidoAtual().getListaItens().size(); i++) {            
            if (jtabelaPedido.getRowCount() <= linhaTabela) {
                modelo.addRow(new String[]{"", ""});
            }
            if(Controlador.getPedidoAtual().getListaItens().get(i) instanceof Acompanhamento){
                Acompanhamento p; //= new Pedido();
                p=(Acompanhamento) Controlador.getPedidoAtual().getListaItens().get(i);
                jtabelaPedido.setValueAt(p.getCodigo(), linhaTabela, 0);
                jtabelaPedido.setValueAt(linhaTabela+1, linhaTabela, 1);
                jtabelaPedido.setValueAt("Acompanhamento", linhaTabela, 2);
                jtabelaPedido.setValueAt(p.getNome(), linhaTabela, 3);
                jtabelaPedido.setValueAt(p.getValor(), linhaTabela, 4);
            }
            else{
                if(Controlador.getPedidoAtual().getListaItens().get(i) instanceof Pizza){
                    Pizza p;
                    p=(Pizza) Controlador.getPedidoAtual().getListaItens().get(i);
                    //jtabelaPedido.setValueAt(p.getCodigo(), linhaTabela, 0);
                    jtabelaPedido.setValueAt(linhaTabela+1, linhaTabela, 1);
                    jtabelaPedido.setValueAt("Pizza", linhaTabela, 2);
                    jtabelaPedido.setValueAt(p.getToStringProduto(), linhaTabela, 3);
                    jtabelaPedido.setValueAt(""+p.getValorPizza(), linhaTabela, 4);
                }
            }
            
            linhaTabela++;
        }
        removeLinhasInutilizadas(linhaTabela);
    }
    
    public void atualizaTabela(){
        imprimeDadosTabelas();
        calculaValorTotalPedido();
    }
    
    public void removeLinhasInutilizadas(int linhaTabela){
        for (int i = linhaTabela; i < jtabelaPedido.getRowCount(); i++) {
            modelo.removeRow(i);
        }
    }
    
    public void calculaValorTotalPedido(){
        float vlrTotal=0;
        for (int i = 0; i < jtabelaPedido.getRowCount(); i++) {
            try{
                vlrTotal+= Float.parseFloat(jtabelaPedido.getValueAt(i, 4).toString());
            }catch(Exception ex){
                System.out.println("Erro ao Calcular o valor total do Pedido "+ex.getMessage());
            }
        }
        
        try{
            vlrTotal-= getFormatoMoeda(jdescontoTF.getText(), "Taxa Entrega");
        }catch(Exception ex){}
        
        try{
            //vlrTotal+= getTaxaEntrega().replace(" ", "");
            vlrTotal+= getFormatoMoeda(jtaxaEntregaTF.getText(), "Taxa Entrega");
        }catch(Exception ex){}
        
        jvalorTotalTF.setText(setFormatoMoeda(""+vlrTotal));
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jadicionarAcompanhamentoB = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jremoverItemB = new javax.swing.JButton();
        jalteraItemB = new javax.swing.JButton();
        jAdicionarPizzaB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtabelaPedido = new javax.swing.JTable();
        jentregaCB = new javax.swing.JCheckBox();
        jconcluirB = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jEnderecoEntrega = new javax.swing.JButton();
        jtaxaEntregaTF = new javax.swing.JFormattedTextField();
        jdescontoTF = new javax.swing.JFormattedTextField();
        jvalorTotalTF = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Novo Pedido");

        jadicionarAcompanhamentoB.setText("Adicionar Acompanhamento");
        jadicionarAcompanhamentoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jadicionarAcompanhamentoBActionPerformed(evt);
            }
        });

        jLabel1.setText("Taxa Entrega:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Valor Total:");

        jremoverItemB.setText("Remover Item");
        jremoverItemB.setPreferredSize(new java.awt.Dimension(165, 23));
        jremoverItemB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jremoverItemBActionPerformed(evt);
            }
        });

        jalteraItemB.setText("Alterar Item");
        jalteraItemB.setPreferredSize(new java.awt.Dimension(165, 23));
        jalteraItemB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jalteraItemBActionPerformed(evt);
            }
        });

        jAdicionarPizzaB.setText("Adicionar Pizza");
        jAdicionarPizzaB.setPreferredSize(new java.awt.Dimension(165, 23));
        jAdicionarPizzaB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAdicionarPizzaBActionPerformed(evt);
            }
        });

        jtabelaPedido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod", "Nº", "Tipo", "Produto", "Valor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtabelaPedido);

        jentregaCB.setText("Entrega");
        jentregaCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jentregaCBActionPerformed(evt);
            }
        });

        jconcluirB.setText("Concluir");
        jconcluirB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jconcluirBActionPerformed(evt);
            }
        });

        jLabel3.setText("Desconto:");

        jEnderecoEntrega.setText("Endereço de Entrega");
        jEnderecoEntrega.setPreferredSize(new java.awt.Dimension(165, 23));
        jEnderecoEntrega.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jEnderecoEntregaActionPerformed(evt);
            }
        });

        jtaxaEntregaTF.setBackground(new java.awt.Color(227, 227, 227));
        jtaxaEntregaTF.setEditable(false);
        try {
            jtaxaEntregaTF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("R$ **#.##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtaxaEntregaTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtaxaEntregaTF.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);
        jtaxaEntregaTF.setPreferredSize(new java.awt.Dimension(83, 20));
        jtaxaEntregaTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtaxaEntregaTFActionPerformed(evt);
            }
        });
        jtaxaEntregaTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtaxaEntregaTFKeyReleased(evt);
            }
        });

        try {
            jdescontoTF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("R$ **#.##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jdescontoTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jdescontoTF.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);
        jdescontoTF.setPreferredSize(new java.awt.Dimension(83, 20));
        jdescontoTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jdescontoTFActionPerformed(evt);
            }
        });
        jdescontoTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jdescontoTFKeyReleased(evt);
            }
        });

        jvalorTotalTF.setEditable(false);
        jvalorTotalTF.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jvalorTotalTF.setPreferredSize(new java.awt.Dimension(83, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 683, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtaxaEntregaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jdescontoTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jvalorTotalTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jentregaCB)
                    .addComponent(jremoverItemB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jalteraItemB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jAdicionarPizzaB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jadicionarAcompanhamentoB, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jEnderecoEntrega, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jconcluirB, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jremoverItemB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jalteraItemB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jAdicionarPizzaB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jadicionarAcompanhamentoB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jEnderecoEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jentregaCB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jtaxaEntregaTF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jdescontoTF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jvalorTotalTF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jconcluirB, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-881)/2, (screenSize.height-409)/2, 881, 409);
    }// </editor-fold>//GEN-END:initComponents

    private void jAdicionarPizzaBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAdicionarPizzaBActionPerformed
        // Adicionar Pizza 
        AddPizza pizza = new AddPizza(this);
        pizza.setVisible(true);
    }//GEN-LAST:event_jAdicionarPizzaBActionPerformed

    private void jadicionarAcompanhamentoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jadicionarAcompanhamentoBActionPerformed
        // Adicionar Acompanhamento:
        AddAcompanhamento acomp = new AddAcompanhamento(this);
        acomp.setVisible(true);
    }//GEN-LAST:event_jadicionarAcompanhamentoBActionPerformed

    private void jEnderecoEntregaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jEnderecoEntregaActionPerformed
        // Endereço de Entrega
        EnderecoEntrega end = new EnderecoEntrega(this);
        end.setVisible(true);
    }//GEN-LAST:event_jEnderecoEntregaActionPerformed

    private void jalteraItemBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jalteraItemBActionPerformed
        // Alterar Item
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                
                if("Acompanhamento".equals(jtabelaPedido.getValueAt(linha, 2).toString())){
                    Acompanhamento ap = (Acompanhamento) Controlador.getPedidoAtual().getListaItens().get(linha);
                    AddAcompanhamento acomp = new AddAcompanhamento(this,ap);
                    
                    acomp.setVisible(true);
                }else{
                    if("Pizza".equals(jtabelaPedido.getValueAt(linha, 2).toString())){
                        Pizza p = (Pizza) Controlador.getPedidoAtual().getListaItens().get(linha);
                        AddPizza pizza = new AddPizza(this,p,linha);
                        pizza.setVisible(true);
                    }else{
                        throw new Exception("Tipo do intem não indentificado!");
                    }
                }
            } else {
                throw new Exception("Selecione a linha do Item desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jalteraItemBActionPerformed

    private void jtaxaEntregaTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtaxaEntregaTFActionPerformed
        // TODO add your handling code here:
        //jtaxaEntregaTF.setText(setFormatoMoeda(jtaxaEntregaTF.getText()));
        calculaValorTotalPedido();
    }//GEN-LAST:event_jtaxaEntregaTFActionPerformed

    private void jtaxaEntregaTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtaxaEntregaTFKeyReleased
        // TODO add your handling code here:
        //setFormatoMoeda();
    }//GEN-LAST:event_jtaxaEntregaTFKeyReleased

    private void jconcluirBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jconcluirBActionPerformed
        // TODO add your handling code here:
        try {
            if(getDadosVisao()){
                if (Controlador.salvar_Pedido(jentregaCB.isSelected())) {
                    this.setVisible(false); 
                    telaPrincipal.setVisible(true);
                    telaPrincipal.atualizaTabela();
                }
            }
            
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Salvar os dados do Pedido!\n"+e.getMessage(), "Erro!", 0);
        }
    }//GEN-LAST:event_jconcluirBActionPerformed

    private void jdescontoTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jdescontoTFActionPerformed
        // TODO add your handling code here:
        calculaValorTotalPedido();
    }//GEN-LAST:event_jdescontoTFActionPerformed

    private void jdescontoTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jdescontoTFKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jdescontoTFKeyReleased

    private void jentregaCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jentregaCBActionPerformed
        // TODO add your handling code here:
        if(jentregaCB.isSelected()){
            jtaxaEntregaTF.setEditable(true);
            jEnderecoEntrega.setEnabled(true);
        }
        else{
            jtaxaEntregaTF.setEditable(false);
            jEnderecoEntrega.setEnabled(false);
        }
    }//GEN-LAST:event_jentregaCBActionPerformed

    private void jremoverItemBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jremoverItemBActionPerformed
        // Remover Item
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                
                Controlador.getPedidoAtual().getListaItens().remove(linha);
                atualizaTabela();
            } else {
                throw new Exception("Selecione a linha do Item desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jremoverItemBActionPerformed

    public Float getTaxaEntrega(){
        String str = jtaxaEntregaTF.getText(); 
        str.replace("R$ ", "");
        str.replace(" ", "");
        return Float.parseFloat(str);
    }
    
    private String setFormatoMoeda(String str){
        try {
            if(str.contains(".")){
                int index = str.indexOf(".");
                String parteInteira = str.substring(0, index);
                String parteFracionaria = str.substring(index+1);
                if(parteFracionaria.length()<2)
                    parteFracionaria+="0";
                if(parteFracionaria.length()>2)
                    parteFracionaria = parteFracionaria.substring(0, 1);
                
                if(parteInteira.length()==3){
                    return "R$ "+parteInteira+"."+parteFracionaria;
                }
                if(parteInteira.length()==2){
                    return "R$  "+parteInteira+"."+parteFracionaria;
                }
                if(parteInteira.length()==1){
                    return "R$   "+parteInteira+"."+parteFracionaria;
                }else{
                    return "R$   0."+parteFracionaria;
                }
            }
            else{
                String parteInteira = str;
                if(parteInteira.length()==3){
                    return "R$ "+parteInteira+"."+".00";
                }
                if(parteInteira.length()==2){
                    return "R$  "+parteInteira+"."+".00";
                }
                if(parteInteira.length()==1){
                    return "R$   "+parteInteira+"."+".00";
                }else{
                    return "R$   0.00";
                }
                
            }   
        } catch (Exception e) {
            System.out.println("Erro ao Formatar estilo Moeda\n"+e.getMessage());
            return "R$  0.00";
        }
        
    }
    
    public Float getFormatoMoeda(String str , String campo){
        Float dinheiro ;
        try {
            str=str.replace("R$ ", "");
            str=str.replace(" ", "");
            
            dinheiro =Float.parseFloat(str);
            return dinheiro;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "O valor do Campo "+campo+"  é Inválido!\n"+e.getMessage(), "Pedido", 0);
            return null;
        }
    }
    
    public String getFormatoMoeda(String str ){
        try {
            str=str.replace("R$ ", "");
            return str.replace(" ", "");
            
        } catch (Exception e) {
            return null;
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAdicionarPizzaB;
    private javax.swing.JButton jEnderecoEntrega;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jadicionarAcompanhamentoB;
    private javax.swing.JButton jalteraItemB;
    private javax.swing.JButton jconcluirB;
    private javax.swing.JFormattedTextField jdescontoTF;
    private javax.swing.JCheckBox jentregaCB;
    private javax.swing.JButton jremoverItemB;
    private javax.swing.JTable jtabelaPedido;
    private javax.swing.JFormattedTextField jtaxaEntregaTF;
    private javax.swing.JTextField jvalorTotalTF;
    // End of variables declaration//GEN-END:variables
}
